<?php

class Logradouros_M extends I_Model {

    protected $_table_name = 'cep_log_index';
    protected $_primary_key = 'id';
    protected $_primary_filter = 'intval';
    protected $_order_by = 'uf';
    public $rules = array();
    protected $_timestamps = FALSE;

    public function __construct() {
        parent::__construct();
    }

    public function getLogradouro($cep) {

        $cep5 = substr($cep, 0, 5);

        $row = $this->get_by(array('cep5' => $cep5), true);
        if ($row):

            $this->_table_name = $row->uf;
            $this->_order_by = 'cidade'; 
            $cep = $cep5 . '-' . substr($cep, 5);
            $irow = $this->get_by(array('cep' => $cep), true);

            if ($irow):
                return array(
                    'CIDADE' => $irow->cidade,
                    'TIPO_LOGRADOURO' => $irow->tp_logradouro,
                    'BAIRRO' => $irow->bairro,
                    'LOGRADOURO' => $irow->logradouro,
                    'UF' => $row->uf);
            endif;
            return false;
        endif;
        return false;
    }

}
